package com.travel.server.manager.impl.command.user;

import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dao.DepartmentInfoDao;
import com.travel.server.manager.dto.request.EmptyRequest;
import com.travel.server.manager.dto.response.user.QueryUserEnumResponse;
import com.travel.server.manager.dto.response.user.QueryUserEnumResponse.UserEnumItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.DepartmentDal;
import com.travel.server.manager.impl.dal.DepartmentDal.DepartmentParam;
import com.travel.server.manager.impl.dal.UserDal;
import com.travel.server.manager.impl.dal.UserDal.UserParam;
import com.travel.server.manager.model.DepartmentInfo;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/30 18:09
 * @Desc:
 */
@Component
@Slf4j
public class QueryUserEnumCommand extends AbstractCommand<EmptyRequest, QueryUserEnumResponse> {

    @Autowired
    private UserDal userDal;

    @Autowired
    private DepartmentDal departmentDal;

    private static final String PRODUCT = "产品部";

    private static final String OPERATOR = "用户运营部";

    @Override
    protected void checkInput(EmptyRequest request) {

    }

    @Override
    public QueryUserEnumResponse handle(EmptyRequest request) {
        QueryUserEnumResponse response = new QueryUserEnumResponse();

        DepartmentInfo productDepartment = selectDepartment(PRODUCT);
        if(productDepartment == null){
            throw new WorkException(WorkCode.PRODUCT_NOT_EXIST);
        }
        DepartmentInfo operatorDepartment = selectDepartment(OPERATOR);
        if(operatorDepartment == null){
            throw new WorkException(WorkCode.OPERATOR_NOT_EXIST);
        }
        List<UserInfo> productUserList = selectByDepartment(productDepartment.getId());
        List<UserInfo> operatorUserList = selectByDepartment(operatorDepartment.getId());

        List<UserEnumItem> productList = makeItemList(productUserList);
        List<UserEnumItem> operatorList = makeItemList(operatorUserList);


        response.setProductList(productList);
        response.setOperatorList(operatorList);
        return response;
    }

    private List<UserEnumItem> makeItemList(List<UserInfo> list){
        List<UserEnumItem> itemList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(list)){
            for(UserInfo userInfo : list){
                UserEnumItem item = new UserEnumItem();
                item.setId(userInfo.getId());
                item.setName(userInfo.getName());
                itemList.add(item);
            }
        }
        return itemList;
    }

    private List<UserInfo> selectByDepartment(Integer departmentId){
        UserParam param = new UserParam();
        param.setDepartmentId(departmentId);
        param.setEnable(CommonConstant.YES);
        return userDal.selectByExample(param);
    }

    private DepartmentInfo selectDepartment(String name){
        DepartmentParam param = new DepartmentParam();
        param.setName(name);
        List<DepartmentInfo> list = departmentDal.selectByExample(param);
        return CollectionUtil.getFirst(list);
    }
}
